package com.jinkaimen.goodcash.ui.activity;

import android.app.ProgressDialog;
import android.view.View;

import com.ht.baselibs.bean.ApkInfoBean;
import com.ht.baselibs.bean.OperatorBean;
import com.ht.baselibs.constant.SPConfig;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.ui.base.BaseOperatorActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.DeviceUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.ht.baselibs.utils.UpdateDialogUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.constant.ConstantPool;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.dialog.TipsDialog;

import retrofit2.Call;
import retrofit2.Response;

/**
 * **************************************************************
 * [好刷刷]
 * **************************************************************
 * Authors: 黄涛 on 17/2/21 17:41
 * QQ：876046815
 * Email:876046815@qq.com
 */
public class ReportMainActivity extends BaseOperatorActivity {
    private TipsDialog mTipsDialog;
    private ProgressDialog mUpdateDialog;
    private boolean isForcedUpdate;

    @Override
    protected View getHeaderView() {
        return null;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("首页").setLeftBtnVisibility(View.GONE);
    }

    @Override
    protected void loadData() {

        //         TODO: 2017/6/26 安卓强制更新
        ApiManager.getApi().getUpdateApkUrl(
                "INV_HSY001020"
        ).enqueue(new NetCallBack<ApkInfoBean>() {
            @Override
            protected void onSuccess(Call<ApkInfoBean> call, Response<ApkInfoBean> response) {
                ApkInfoBean apkInfoBean = response.body();
                if (apkInfoBean.isUpgrade != 0 && DeviceUtils.getAppVersionCode() < apkInfoBean.versionCode) {
                    String newVersionName = apkInfoBean.versionName;
                    String ignoreVersionName = SharedPreferencesUtils.getString(SPConfig.IGNORE_APP_UPDATE_VERSION, "");
                    if ("".equals(ignoreVersionName) || !ignoreVersionName.equals(newVersionName)) {

                        String[] versionStr = newVersionName.split("\\.");

                        isForcedUpdate = versionStr.length >= UpdateDialogUtils.FORCED_UPDATE_LENG;

                        mUpdateDialog = new ProgressDialog(ReportMainActivity.this);
                        mUpdateDialog.setTitle("下载最新安装包");
                        mUpdateDialog.setMessage("正在下载，请稍后...");
                        mUpdateDialog.setMax(100);
                        mUpdateDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

//                        apkInfoBean.versionUrl = "http://192.168.1.37:8080/app1.0.1.apk";

                        UpdateDialogUtils.showUpdateDialog(ReportMainActivity.this, apkInfoBean, mUpdateDialog);
                    }
                }
            }
        });
        add(true, R.mipmap.sign_self_frag, "添加报单", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                ActivityUtils.startActivity(ReportMainActivity.this, RegisterActivity.class);
            }
        });

        add(true, R.mipmap.sign_self_frag, "已通过商户", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                ActivityUtils.startActivity(ReportMainActivity.this, PassShoperManagerActivity.class);
            }
        });

        add(true, R.mipmap.sign_self_frag, "商户管理", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                ActivityUtils.startActivity(ReportMainActivity.this, ShoperManagerActivity.class);
            }
        });

        add(true, R.mipmap.sign_self_frag, "下载客户端", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                ActivityUtils.startActivity(ReportMainActivity.this, QrDownLoadActivity.class);
            }
        });
//        add(true, R.mipmap.sign_self_frag, "测试页面", new OperatorBean.OnItemClickListener() {
//            @Override
//            public void onClick(boolean isChecked) {
//                ActivityUtils.startActivity(ReportMainActivity.this, QrDownLoadActivity.class);
//            }
//        });


        add(true, R.mipmap.setting_self_frag, "退出登陆", new OperatorBean.OnItemClickListener() {
            @Override
            public void onClick(boolean isChecked) {
                mTipsDialog = new TipsDialog(ReportMainActivity.this);
                mTipsDialog.setContent("是否退出登录");
                mTipsDialog.show();
                mTipsDialog.setOnDialogTipsClickListener(new TipsDialog.OnDialogTipsClickListener() {

                    @Override
                    public void doEnter() {
                        mTipsDialog.dismiss();
                        SharedPreferencesUtils.put(ConstantPool.REPORT_ACCOUT_ID, (long) 0);
                        ActivityUtils.startActivityAndFinish(ReportMainActivity.this, LoginActivity.class);
                    }

                    @Override
                    public void doCancel() {
                        mTipsDialog.dismiss();
                    }
                });
            }
        });
    }

    @Override
    protected void onEvent(int code, Object data) {
        switch (code){

            case EventCode.EVENT_CODE_UPDATE_APK_PROGRESS:
                mUpdateDialog.setProgress((Integer) data);
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_END:
                DeviceUtils.installApk(this, (String)data);
                if (!isForcedUpdate){
                    mUpdateDialog.dismiss();
                }


                //
                break;
            case EventCode.EVENT_CODE_UPDATE_APK_ERROR:
                UIUtils.showToast("您当前的网络不稳定，下载安装包失败");
                mUpdateDialog.dismiss();
                break;
        }
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }
}
